// pages/card-exchange/card-exchange.js
var util = require("../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowFreight: !1,
        showTipAddress: !0,
        maxStock:99,
        isSubmit:false,//防止表单重新提交
        coupon: {
            value:0.00,
            totalLength: 0,
            canUseCoupons: [],
            noCanUseCoupons: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if(wx.getStorageSync("addressList")){
            var addressList = JSON.parse(wx.getStorageSync("addressList"));
        }else{
            var addressList = [];
        }
        this.setData({
            addressList: addressList
        });
        this.getCardInfo(options.id)
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        
    },
    toAddress: function(e) {
        wx.navigateTo({
            url: "/pages/select-address/select-address?type=card&id=" + e.currentTarget.dataset.id + '&pickNum=' + e.currentTarget.dataset.num +'&index=' + e.currentTarget.dataset.index + '&time=' + e.currentTarget.dataset.time
        })
    },
    delAddress: function (e) {
        var index = e.currentTarget.dataset.index;
        var addressList = this.data.addressList;
        addressList.splice(index,1);
        wx.setStorageSync("addressList", JSON.stringify(addressList));
        this.setData({
            addressList: addressList
        });
    },
    getCardInfo: function (val) {
        const _this = this
        util.request(getApp().api.card.cardInfo,{
            waterCardId: val,
        }).then(
        function(res) {
            if (res.code == 200) {
                _this.setData({
                    card_info: res.data[0]
                });
                var cardType = _this.data.card_info.cardType;
                _this.setData({
                    cardNum:cardType==0? 1: cardType==1? 3 : 12
                });
                wx.setStorageSync("orderItem", JSON.stringify({
                    productNum: _this.data.card_info.waterUseNum, //产品数量
                }));
            } else {
            wx.showToast({
                title: res.msg, //提示的内容,
                icon: "none", //图标,
                //duration: 1500, //延迟时间,
                mask: false, //显示透明蒙层，防止触摸穿透,
            });
            setTimeout(function() {
                wx.hideToast();
            }, 1500);
            return
            }
        });
    },
    toPay: function () {
        if(this.data.addressList.length==0){
            wx.showToast({
                title: '请选择地址', //提示的内容,
                icon: "none", //图标,
                //duration: 1500, //延迟时间,
                mask: false, //显示透明蒙层，防止触摸穿透,
            });
            return
        }
        var cardNum = this.data.cardNum;
        var realNum = this.data.addressList.length;
        if(cardNum>=realNum) {
            var deliveryFee = 0
        }else{
            var deliveryFee = (realNum-cardNum)*50
        }
        var total = 0
        this.data.addressList.forEach(item=>{
            total +=item.num
        })
        if(total<5){
            wx.showToast({
                title: '最少提取5箱', //提示的内容,
                icon: "none", //图标,
                //duration: 1500, //延迟时间,
                mask: false, //显示透明蒙层，防止触摸穿透,
            });
            return
        }
        const _this = this
        util.request(getApp().api.wxpay.pay,{
            openId: wx.getStorageSync('openid'),
            orderType: 3,
            waterId: this.data.card_info.waterCardId,
            address: this.data.addressList,
            baseId: this.data.card_info.productId,
            productNum: total,
            orderAmount: 0,
            deliveryNum: this.data.addressList.length,
            freight: deliveryFee,
            productHeft: 0
        },'post').then(
        function(res) {
            if (res.code == 200) {
                wx.removeStorageSync('addressList')
                wx.navigateTo({
                    url: '/pages/order-list/order-list?id=0'
                })
            } else {
            wx.showToast({
                title: res.msg, //提示的内容,
                icon: "none", //图标,
                //duration: 1500, //延迟时间,
                mask: false, //显示透明蒙层，防止触摸穿透,
            });
            setTimeout(function() {
                wx.hideToast();
            }, 1500);
            return
            }
        });
        // 做任务（线上下单）
        util.request(getApp().api.butler.insertStatustics,{
            type: 1,
            knowledgeId: 0,
            waterDropId: 5
            },'post').then(
            function(res) {
                if (res.code == 200) {
                
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    showFreightRule: function() {
        this.setData({
            isShowFreight: !0
        })
    },
    closeFreight: function() {
        this.setData({
            isShowFreight: !1
        })
    },
    openAdress: function() {
        this.setData({
            showTipAddress: !0
        })
    },
    closeAdress: function() {
        this.setData({
            showTipAddress: !1
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    }
});